<?php
/**
 * 图书分类管理控制器
 * 
 * @author sandy
 *
 */
class categoryApp extends adminControl{
	
	protected function _init(){
		$this->lang->load('category');
	}
	
	/**
	 * 分类列表
	 */
	public function index(){
		$parent_cat_id = isset($_REQUEST['pid']) ? intval($_REQUEST['pid']) : 0;
		
		$category_mod = &get_model('category');
		
		// 获取当前页码及分页配置
		$page_info = get_page(20);
		
		$categories = $category_mod->get_category($parent_cat_id,$page_info);
		
		// 树形结构
		$category_tree = array();
		$site_url = $this->config->get_item('site_url');
		foreach($categories['result'] as $key => $row){
			$row['id'] = $row['category_id'];
			$row['parentid'] = $row['parent_cat_id'];
			if ($row['status'] == 0){
				$row['status_str'] = '启用';
			} else {
				$row['status_str'] = '禁用';
			}
			$row['str_manage'] = '';
			//$row['str_manage'] .= '<a href="'.creat_url('c=category&a=del&id='.$row['category_id'],$site_url,0,false).'" title="管理子分类">管理子分类</a>';
			$row['str_manage'] .= '<a href="'.creat_url('c=category&a=edit&id='.$row['category_id'],$site_url,0,false).'" title="编辑">编辑</a>';
			$row['str_manage'] .= '<a href="'.creat_url('c=category&a=del&id='.$row['category_id'],$site_url,0,false).'" title="删除">删除</a>';
		
			$category_tree[$key] = $row;
		}
		//dump($category_tree);
		$srt ="<tr>
				<td><input type='checkbox' /></td>
				<td>\$category_id</td>
				<td>\$spacer\$category</td>
				<td>\$depth</td>
				<td>\$status_str</td>
				<td>\$str_manage</td>
			  </tr>";
		
		import_lib('tree');
		$tree = new tree();
		$tree->init($category_tree);
		$category_tree = $tree->get_tree(0,$srt);
		
		// 获取分页信息
		$page_info['total'] = $categories['total'];
		$url = "index.php?c=category";
		$pages_data = format_page($page_info['total'], $page_info['cur_page'], $url, $page_info['per_page'],5);
		$pages = get_page_html($pages_data);
		
		$this->assign('categories',$categories['result']);
		$this->assign('category_tree', $category_tree);
		$this->assign('pages',$pages);
		$this->_title = $this->lang->get('html_title') . ' - ' . $this->lang->get('app_name');
		$this->display();
	}
	
	/**
	 * 返回分类 json 格式数据
	 */
	public function grid(){
		$this->add_css('style');
		$this->add_css('menu');
		//$this->add_css('form');
		$this->add_js('grid.locale-cn');
		$this->add_js('jquery.jqGrid.min');
		$this->add_js('category');

		$this->display('category_grid.html.php');
	}
	
	// 返回分类 json 格式数据
	public function gridajax(){
		
		$page = $_GET['page']; // get the requested page
		$limit = $_GET['rows']; // get how many rows we want to have into the grid
		
		$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
		$sord = $_GET['sord']; // get the direction
		
		$order_by = '';
		if($sidx){
			switch($sidx){
				case 'id':
					$order_by = 'ORDER BY category_id';
				break;
			}
			
			if($sord){
				$order_by .= ' '.$sord;
			}
		}
		
		$category_mod = &get_model('category');
		
		// 获取当前页码及分页配置
		$page_info = get_page($limit);
		//dump($page_info,$order_by);
		$categories = $category_mod->get_category($page_info['offset'],$page_info['limit'],$order_by);
		
		$responce = new stdClass();
		
		$responce->page = $page; 
		$responce->total = ceil($categories['total'] / $limit); 
		$responce->records = $categories['total'];
		
		foreach ($categories['result'] as $key => $category){
			$responce->rows[$key]['id'] = $key + 1;
			$responce->rows[$key]['cell'] = array($category['category_id'],$category['category'],$category['parent_cat_id'] + 1);
		}
		
		echo json_encode($responce);
	}
	
	/**
	 * 添加分类,提供ajax和普通请求两种接口
	 */
	public function add(){
		
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		if ($dosubmit){
			$parent_id = intval($_POST['parent_id']);
			$category = trim($_POST['category']);
			
			$category_info = array(
					'category' => $category,
					'parent_cat_id' => $parent_id
			);
	
			$category_mod = &get_model('category');
			
			if ($parent_id == 0){
				$category_info ['depth'] = 0;
			} else {
				
				$parent_info = $category_mod->get_category_info($parent_id);
				$category_info ['depth'] = $parent_info['depth'] + 1;
			}
			
			if($category_mod->add_category($category_info)){
				$this->show_message( "添加分类成功!",$this->config->get_item('site_url').'/index.php?c=category');
			} else {
				$this->show_message("添加分类失败!",'');
			}
			
		} else {
			
			$category_mod = &get_model('category');
			$categories = $category_mod->get_category();
			
			// 树形结构
			$category_tree = $category_mod->get_category_tree($categories['result']);

			$this->assign('categories',$categories['result']);
			$this->assign('category_tree', $category_tree);
			$this->display();
		}
	}
	
	/**
	 * 编辑分类,提供ajax和普通请求两种接口
	 */
	public function edit(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : "";
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		if ($dosubmit){			
			$parent_id = intval($_POST['parent_id']);
			$category = trim($_POST['category']);
				
			$category_info = array(
					'category_id' => $id,
					'category' => $category,
					'parent_cat_id' => $parent_id
			);
			
			$category_mod = &get_model('category');
			$parent_info = $category_mod->get_category_info($parent_id);
			$category_info['depth'] = $parent_info['depth'] + 1;
			
			
			if($category_mod->update_category($category_info)){
				$this->show_message( "编辑分类成功!",$this->config->get_item('site_url').'/index.php?c=category');
			} else {
				$this->show_message("编辑分类失败!",$this->config->get_item('site_url').'/index.php?c=category');
			}
			
		} else {
			$category_mod = &get_model('category');
			
			$categories = $category_mod->get_category();
			$categories = specify_array_key($categories['result'],'category_id');
			
			$category_info = $categories[$id];
			$category_tree = $category_mod->get_category_tree($categories,$category_info['parent_cat_id']);
			
			$this->assign('category_info',$category_info);
			$this->assign('category_tree', $category_tree);
			$this->display();
		}
		
	}
	
	/**
	 * 删除分类,提供ajax和普通请求两种接口
	 */
	public function del(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : ""; 
		$category_mod = &get_model('category');
		
		if($category_mod->del_category($id)){
			$this->show_message( "删除分类成功!",$this->config->get_item('site_url').'/index.php?c=category');
		} else {
			$this->show_message( "删除分类失败!",$this->config->get_item('site_url').'/index.php?c=category');
		}
	}

}

